﻿using com.fmtx.IDal;
using fmtx.Framework.Cache;
using fmtx.Framework.DALFactory;
using fmtx.Framework.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;

namespace website.Areas.Api.Controllers
{
    public class AreaController : Controller
    {
        //
        // GET: /Api/Area/

        [OutputCache(Duration=24*60*60)]
        public JavaScriptResult Index()
        {
            try
            {
                DataCache cache = new DataCache(CacheKey.FILE_AREA);
                var keys = cache.Get() as List<AreaKey>;
                if (keys == null)
                {
                    IPlace _place = DataAccess<IPlace>.GetObject();
                    ICity _city = DataAccess<ICity>.GetObject();
                    IProvince _province = DataAccess<IProvince>.GetObject();
                    ICountry _country = DataAccess<ICountry>.GetObject();
                    keys = new List<AreaKey>();
                    var plist = _place.QueryIdAndName();
                    foreach (var item in plist)
                    {
                        if (keys.Where(w => w.Name == item.Name && w.Type == AreaType.Place).Count() == 0)
                        {
                            keys.Add(new AreaKey(item.Id, item.Name, AreaType.Place, "/place/" + item.Id));
                        }
                    }
                    var clist = _city.QueryIdAndName();
                    foreach (var item in clist)
                    {
                        if (keys.Where(w => w.Name == item.Name && w.Type == AreaType.City).Count() == 0)
                        {
                            keys.Add(new AreaKey(item.Id, item.Name, AreaType.City, "/city/" + item.Id));
                        }
                    }
                    var pplist = _province.QueryIdAndName();
                    foreach (var item in pplist)
                    {
                        if (keys.Where(w => w.Name == item.Name && w.Type == AreaType.Province).Count() == 0)
                        {
                            keys.Add(new AreaKey(item.Id, item.Name, AreaType.Province, "/Province/" + item.Id));
                        }
                    }
                    var cclist = _country.QueryIdAndName();
                    foreach (var item in cclist)
                    {
                        if (keys.Where(w => w.Name == item.Name && w.Type == AreaType.Country).Count() == 0)
                        {
                            keys.Add(new AreaKey(item.Id, item.Name, AreaType.Country, "/country/" + item.Id));
                        }
                    }
                    if (keys != null && keys.Count != 0)
                    {
                        cache.Set(keys, 720);
                    }
                }

                return JavaScript("var arr=" + JsonConvert.SerializeObject(keys) + ";$('#conter').textSearch()");
            }
            catch (Exception ex)
            {
                fmtx.Framework.Log.Error(ex);
                return JavaScript("var arr=[]");
            }
        }

    }
}
